<script type="text/javascript">
var $datatable;
var $datatable_export_bill;
var _DATATABLE_TR;
var oTable;

var _IMPORT_DATE_FROM = '';
var _IMPORT_DATE_TO = '';
var _EXPORT_BILL_CODE = '';
var _EXPORT_DATE_FROM = '';
var _EXPORT_DATE_TO = '';
    
$(document).ready(function() {
    $datatable = $('#datatable_dvt');
    $datatable_export_bill = $('#datatable_export_bill');
    $datatable_construction = $('#datatable_construction');
    //$dialog_form_edit = $('#dialog_form_edit');
    //$dialog_form_add = $('#dialog_form_add');
    //$dialog_confrim_delete = $('#dialog_confrim_delete');
    
    oTable = $datatable.dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bInfo": false,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": false,
        "bFilter" : false,
        "bLengthChange": false,
        //"bFilter": false,
        "sAjaxSource": $datatable.attr('active_url'),
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            aoData.push( { "name": "filter_date_from", "value": _IMPORT_DATE_FROM } );
            aoData.push( { "name": "filter_date_to", "value": _IMPORT_DATE_TO } );
            
            $.getJSON( sSource, aoData, function (json) { 
                fnCallback(json)
            } );

        },
        "fnDrawCallback": function( oSettings ) {
            //$("#datatable_nb_wrapper").find(".fg-toolbar").removeClass("ui-widget-header");
            datatable_selected();
            _DATATABLE_TR = null;
        },
        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            if( aData['id'] != undefined ){
                $(nRow).attr("value_id",aData['id']);
            }
            if( aData['modify'] != undefined ){
                $(nRow).attr("modify",aData['modify']);
            }
            //if ( aData[4] == "A" ){$('td:eq(4)', nRow).html( '<b>A</b>' );}
            return nRow;
        }
        //"oTableTools": {"aButtons": ["copy", "csv", "xls", "pdf"]}
        
    });
    
    oTable_exportBill = $datatable_export_bill.dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bInfo": false,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": false,
        "bFilter" : false,
        "bLengthChange": false,
        //"bFilter": false,
        "sAjaxSource": $datatable_export_bill.attr('active_url'),
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            aoData.push( { "name": "filter_bill_code", "value": _EXPORT_BILL_CODE } );
            aoData.push( { "name": "filter_date_from", "value": _EXPORT_DATE_FROM } );
            aoData.push( { "name": "filter_date_to", "value": _EXPORT_DATE_TO } );
            
            $.getJSON( sSource, aoData, function (json) { 
                fnCallback(json)
            } );

        },
        "fnDrawCallback": function( oSettings ) {
            //$("#datatable_nb_wrapper").find(".fg-toolbar").removeClass("ui-widget-header");
            datatable_selected();
        },
        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            if( aData['id'] != undefined ){
                $(nRow).attr("value_id",aData['id']);
            }
            if( aData['modify'] != undefined ){
                $(nRow).attr("modify",aData['modify']);
            }
            //if ( aData[4] == "A" ){$('td:eq(4)', nRow).html( '<b>A</b>' );}
            return nRow;
        }
        //"oTableTools": {"aButtons": ["copy", "csv", "xls", "pdf"]}
        
    });
    
    oTable_construction = $datatable_construction.dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bInfo": false,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": false,
        "bFilter" : false,
        "bLengthChange": false,
        //"bFilter": false,
        "sAjaxSource": $datatable_construction.attr('active_url'),
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            aoData.push( { "name": "filter_bill_code", "value": _EXPORT_BILL_CODE } );
            aoData.push( { "name": "filter_date_from", "value": _EXPORT_DATE_FROM } );
            aoData.push( { "name": "filter_date_to", "value": _EXPORT_DATE_TO } );
            
            $.getJSON( sSource, aoData, function (json) { 
                fnCallback(json)
            } );

        },
        "fnDrawCallback": function( oSettings ) {
            //$("#datatable_nb_wrapper").find(".fg-toolbar").removeClass("ui-widget-header");
            datatable_selected();
        },
        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            if( aData['id'] != undefined ){
                $(nRow).attr("value_id",aData['id']);
            }
            if( aData['modify'] != undefined ){
                $(nRow).attr("modify",aData['modify']);
            }
            //if ( aData[4] == "A" ){$('td:eq(4)', nRow).html( '<b>A</b>' );}
            return nRow;
        }
        //"oTableTools": {"aButtons": ["copy", "csv", "xls", "pdf"]}
        
    });
    
    $(function() {
        $( "#tabs" ).tabs();
    });
    $(function() {
        $( ".datepicker" ).datepicker({
            dateFormat: "dd-mm-yy",//"yy-mm-dd",
            showOn: "button",
            buttonImage: "<?php print base_url('template/ess/images/calendar_icon.gif'); ?>",
            buttonImageOnly: true,
            changeMonth: true,
            changeYear: true,
            monthNamesShort: [ "<?php print lang('monthshort_jan'); ?>", "<?php print lang('monthshort_feb'); ?>", "<?php print lang('monthshort_mar'); ?>", "<?php print lang('monthshort_apr'); ?>", "<?php print lang('monthshort_maj'); ?>", "<?php print lang('monthshort_jun'); ?>", "<?php print lang('monthshort_jul'); ?>", "<?php print lang('monthshort_aug'); ?>", "<?php print lang('monthshort_sep'); ?>", "<?php print lang('monthshort_okt'); ?>", "<?php print lang('monthshort_nov'); ?>", "<?php print lang('monthshort_dec'); ?>" ],
            dayNamesMin: [ "<?php print lang('dayshort_sun'); ?>", "<?php print lang('dayshort_mon'); ?>", "<?php print lang('dayshort_tue'); ?>", "<?php print lang('dayshort_wed'); ?>", "<?php print lang('dayshort_thu'); ?>", "<?php print lang('dayshort_fri'); ?>", "<?php print lang('dayshort_sat'); ?>" ]
        });
    });
    
    filter_datatable_import();
    filter_datatable_export();
    /*dialogFormAddNew();
    dialogConfrimDelete();
    dialogFormEdit();
    dialogError();*/
} );

function datatable_selected(){
    $datatable.find('tbody tr[modify=edit]').click( function( e ) {
        if ( $(this).hasClass('row_selected') ) {
            $(this).removeClass('row_selected');
            _DATATABLE_TR = null;
        }else {
            oTable.$('tr.row_selected').removeClass('row_selected');
            $(this).addClass('row_selected');
            _DATATABLE_TR = $(this).attr('value_id');
        }
    });
    
    $datatable.find('tbody tr[modify=edit]').bind("dblclick", function(){
        if ( !$(this).hasClass('row_selected') ) {
            $(this).addClass('row_selected');
        }
        _DATATABLE_TR = $(this).attr('value_id');
        even_edit_dbclick( _DATATABLE_TR );

    });

}

function even_edit_dbclick(id){
    var url = $dialog_form_edit.attr('active_url') + '/' + id;
        
    $.get(url, function(data) {
        $dialog_form_edit.html( data );
        $dialog_form_edit.dialog( "open" );
    });
}
/*
function dialogFormAddNew(){
    
    $dialog_form_add.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "<?php print lang('btn_save') ?>": function() {
                var $this = $( this );
                $("#frm_add").ajaxSubmit({
                    //beforeSubmit: validate
                    dataType:  'json',
                    beforeSubmit: function(formData, jqForm, options) {
                        return validate_frm();
                    },
                    success: function(data) {
                        if(data.action == 'insert'){
                            //oTable.fnDraw();
                            //oTable.fnFilter( data.ten );
                            oTable.fnSort( [ [0,'desc'] ] );
                        }
                        $this.dialog( "close" );
                    }
                });
            },
            "<?php print lang('btn_cancel') ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            $dialog_form_add.html('');
        }
    });
        
}

function dialogFormEdit(){
    
    $dialog_form_edit.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "<?php print lang('btn_save') ?>": function() {
                var $this = $( this );
                $("#frm_add").ajaxSubmit({
                    //beforeSubmit: validate
                    dataType:  'json',
                    beforeSubmit: function(formData, jqForm, options) {                        
                        return validate_frm();
                    },
                    success: function(data) {
                        if(data.action == 'update'){
                            var $tr = oTable.$('tr[value_id="'+ data.id +'"]');
                            $tr.find('td:eq(1)').html(data.name);
                            $tr.find('td:eq(2)').html(data.status);
                        }
                        $this.dialog( "close" );
                    }
                });
            },
            "<?php print lang('btn_cancel') ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            $dialog_form_edit.html('');
        }
    });
        
}

function validate_frm(){
    var flag = true;
    var name = $('input[name=name]').val();
    if(name == ''){
        $("input[name=name]").addClass("i-error");
        $("#iname").show();
        flag = false;
    }else{
        $("#iname").hide();
        $("input[name=name]").removeClass("i-error");
    }
    
    return flag;
}

function dialogConfrimDelete(){
    
    $dialog_confrim_delete.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "Delete": function() {
                var url = $dialog_confrim_delete.attr('active_url');
                
                $.post(url, { id: _DATATABLE_TR },function(data){
                     _DATATABLE_TR = null;
                     oTable.fnDraw();
                }, "json");
                
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            //allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });
        
}

function even_add(){
    var url = $dialog_form_add.attr('active_url');
    $.get(url, function(data) {
        $dialog_form_add.html( data );
        $dialog_form_add.dialog( "open" );
    });
}

function even_edit(){
    if(_DATATABLE_TR != null){
        var url = $dialog_form_edit.attr('active_url') + '/' + _DATATABLE_TR;
        $.get(url, function(data) {
            $dialog_form_edit.html( data );
            $dialog_form_edit.dialog( "open" );
        });
    }else{
        $dialog_error.html( '<?php print lang('error_noselected'); ?>' );
        $dialog_error.dialog( "open" );
    }
}

function even_delete(){
    if(_DATATABLE_TR != null){
        $dialog_confrim_delete.dialog( "open" );
    }else{
        $dialog_error.html( '<?php print lang('error_noselected'); ?>' );
        $dialog_error.dialog( "open" );
    }
}
*/

function filter_datatable_import(){
    _IMPORT_DATE_FROM = $('input#import_date_from').val();
    _IMPORT_DATE_TO = $('input#import_date_to').val();
    oTable.fnDraw();
}
function filter_datatable_export(){
    _EXPORT_BILL_CODE = $('input#export_bill_code').val();
    _EXPORT_DATE_FROM = $('input#export_date_from').val();
    _EXPORT_DATE_TO = $('input#export_date_to').val();
    oTable_exportBill.fnDraw();
}

function submitFormReportDate(){
    $('#frm_report_date').submit();
}
</script>

<div class="warpp">

    <div id="tabs">
        <ul>
            <li><a href="#tabs-1"><?php print lang('vt_tabnhapkho'); ?></a></li>
            <li><a href="#tabs-2"><?php print lang('vt_tabxuatkho'); ?></a></li>
            <li><a href="#tabs-3"><?php print lang('vt_constructionlist'); ?></a></li>
            <li><a href="#tabs-4"><?php print lang('vt_tabbaocao'); ?></a></li>
        </ul>
        <div id="tabs-1">
            <div>
                <p>
                <label><?php print lang('vt_tungay'); ?></label>: <input type="text" value="<?php print date('d-m-Y'); ?>" name="import_date_from" id="import_date_from" class="datepicker" />
                <label><?php print lang('vt_denngay');?></label>: <input type="text" name="import_date_to" id="import_date_to" class="datepicker" />
                <button onclick="filter_datatable_import()";><?php print lang('btn_filter'); ?></button>
                </p>
            </div>
            <div>
                <table cellpadding="0" cellspacing="0" border="0" class="display" id="datatable_dvt" active_url="<?php print site_url('rp/datatable_filter_bill_import/'); ?>">
                    <thead>
                        <tr>
                            <th width="40px"><?php print lang('vt_stt'); ?></th>
                            <th width="100px"><?php print lang('vt_billcode'); ?></th>
                            <th width="200px"><?php print lang('vt_nguoinhap'); ?></th>
                            <th><?php print lang('vt_nhacungcap'); ?></th>
                            <th><?php print lang('vt_ngaynhap'); ?></th>
                            <th><?php print lang('vt_thanhtien'); ?></th>
                            <th><?php print lang('vt_note'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="5" class="dataTables_empty">Loading data from server</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div id="tabs-2">
            <div>
                <p>
                <label><?php print lang('vt_billcode'); ?></label>: <input type="text" name="export_bill_code" id="export_bill_code" class="" />
                <label><?php print lang('vt_tungay'); ?></label>: <input type="text" value="<?php print date('d-m-Y'); ?>" name="export_date_from" id="export_date_from" class="datepicker" />
                <label><?php print lang('vt_denngay'); ?></label>: <input type="text" name="export_date_to" id="export_date_to" class="datepicker" />
                <button onclick="filter_datatable_export()";><?php print lang('btn_filter'); ?></button>
                </p>
            </div>
            <div>
                <table cellpadding="0" cellspacing="0" border="0" class="display" id="datatable_export_bill" active_url="<?php print site_url('rp/datatable_filter_bill_export/'); ?>">
                    <thead>
                        <tr>
                            <th width="40px"><?php print lang('vt_stt'); ?></th>
                            <th width="100px"><?php print lang('vt_billcode'); ?></th>
                            <th width="200px"><?php print lang('vt_nguoinhap'); ?></th>
                            <th><?php print lang('vt_nguoinhanphieu'); ?></th>
                            <th><?php print lang('vt_ngayxuat'); ?></th>
                            <th><?php print lang('vt_thanhtien'); ?></th>
                            <th><?php print lang('vt_note'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="5" class="dataTables_empty">Loading data from server</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
        </div>
        <div id="tabs-3">
            <div>
                <table cellpadding="0" cellspacing="0" border="0" class="display" id="datatable_construction" active_url="<?php print site_url('vt/datatable_construction'); ?>">
                    <thead>
                        <tr>
                            <th width="40px"><?php print lang('vt_stt'); ?></th>
                            <th><?php print lang('vt_constructionname'); ?></th>
                            <th><?php print lang('vt_createddate'); ?></th>
                            <th><?php print lang('vt_enddate'); ?></th>
                            <th><?php print lang('vt_trangthai'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="5" class="dataTables_empty">Loading data from server</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div id="tabs-4">
            <div>
                <form id="frm_report_date" action="<?php print site_url('rp/report'); ?>" method="post">
                <p>
                    <label><?php print lang('vt_tungay'); ?></label>: <input type="text" value="<?php print date('d-m-Y'); ?>" name="report_date_from" id="report_date_from" class="datepicker" />
                    <label><?php print lang('vt_denngay'); ?></label>: <input type="text" name="report_date_to" id="report_date_to" class="datepicker" />
                    <button onclick="submitFormReportDate()";><?php print lang('btn_filter'); ?></button>
                </p>
                </form>
            </div>
        </div>
    </div>
    
    
</div>

<!--<div id="dialog_form_add" title="<?php print lang('vt_title_themdvt'); ?>" active_url="<?php print site_url('vt/ajax_loadfrm_dvt/'); ?>"></div>
<div id="dialog_form_edit" title="<?php print lang('vt_title_suadvt'); ?>" active_url="<?php print site_url('vt/ajax_loadfrm_dvt/'); ?>"></div>
<div id="dialog_confrim_delete" title="<?php print lang('title_thongbao'); ?>" active_url="<?php print site_url('vt/delete_dvt/'); ?>">
    <p><?php print lang('msg_delete'); ?></p>
</div>
-->